/**
 * 
 */
package aiproject3.predictors;

import java.util.ArrayList;

import aiproject3.models.KnowledgeBase;

/**
 * @author Chris
 *
 */
public class NGramPredictor<U> implements Predictor<U> {

	private U _currentPrediction;
	private ArrayList<U> _unitsSeen;
	private KnowledgeBase<U, Integer> _model;
	
	public NGramPredictor(KnowledgeBase<U, Integer> model) {
		this._model = model;
		_unitsSeen = new ArrayList<U>();
	}
	
	
	public U predictNext() {
		return _currentPrediction;
	}

	
	public void updatePredictor(U unit) {
		if (!_unitsSeen.contains(unit))
			_unitsSeen.add(unit);
		
		if (_currentPrediction == null)
			_currentPrediction = unit;
		else {
			int maxValue = (_model.getValueFromModel(_currentPrediction) == null) ? 0 : _model.getValueFromModel(_currentPrediction);
			for (U u : _unitsSeen) {
				if (_model.contains(u)) {
					if (_model.getValueFromModel(u) > maxValue) {
						_currentPrediction = u;
						maxValue = _model.getValueFromModel(u);
					}
				}
			}
		}
	}

}
